import 'package:flutter/material.dart';
import 'package:toast/toast.dart';
import '/common/widget_factory.dart';

class RegisterPage extends StatefulWidget {
  const RegisterPage({super.key});

  @override
  State<StatefulWidget> createState() {
    return RegisterPageState();
  }
}

class RegisterPageState extends State<RegisterPage> {
  final TextEditingController _accountController = TextEditingController();
  final TextEditingController _passwordController = TextEditingController();
  final TextEditingController _repasswordController = TextEditingController();

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
      appBar: AppBar(
        title: const Text("注册"),
      ),
      body: Stack(
        alignment: Alignment.center,
        children: <Widget>[
          Positioned(
            top: 150,
            width: 300,
            height: 270,
            child: Column(
              children: <Widget>[
                WidgetFactor.buildTextField(controller: _accountController, hintText: "请输入账号"),
                Flexible(
                  flex: 1,
                  child: Container(),
                ),
                WidgetFactor.buildTextField(controller: _passwordController, hintText: "请输入密码", obscureText: true),
                Flexible(
                  flex: 1,
                  child: Container(),
                ),
                WidgetFactor.buildTextField(controller: _repasswordController, hintText: "请确认密码", obscureText: true),
                Flexible(
                  flex: 3,
                  child: Container(),
                ),
                WidgetFactor.buildButton(onPressed: _clickRegisterBtn, title: "注册"),
              ],
            ),
          ),
        ],
      ),
    );
  }

  void _clickRegisterBtn() {
    if (_accountController.text.trim().isEmpty) {
      Toast.show("请输入账号");
    } else if (_passwordController.text.trim().isEmpty) {
      Toast.show("请输入密码");
    } else if (_passwordController.text.trim() != _repasswordController.text.trim()) {
      Toast.show("密码不一致");
    } else {
      _register(_accountController.text.trim(), _passwordController.text.trim());
    }
  }

  void _register(String account, String password) {
    Map<String, dynamic> param = {"username": account, "password": password, "repassword": password};
    // NetWorkUtil().GET(Api.register, param, (success) {
    //   Toast.show("注册成功");
    //   Navigator.of(context).pop();
    // }, (error) {
    //   Toast.show(error);
    // });
  }

  @override
  void dispose() {
    _accountController.dispose();
    _passwordController.dispose();
    _repasswordController.dispose();
    super.dispose();
  }
}
